package com.fp.voiceshoper.exp;
 
import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.os.Parcelable;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences; 
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ListAdapter;
import android.widget.Toast;
import com.fp.fpShoper.R;
import com.fp.voiceshoper.db.DbOpenHelper;
import com.fp.voiceshoper.items.ShopItemData;
import com.fp.voiceshoper.utils.Log;

public class ExpandablelistActivity extends Activity {
	final Context context = this;
    private ExpandableListView expandableListView;
    Button button1;
    private  ArrayList<ShopItemData> models = new ArrayList<ShopItemData>(0);
    private  ArrayList<ShopItemData> result = new ArrayList<ShopItemData>(0);
    DbOpenHelper dbOpenHelper;
    SampleExpandableListAdapter adapt;
    
  
    
    private SampleExpandableListAdapter loadFavoritesItem(){
    	List<ShopItemData[]> data = new ArrayList<ShopItemData[]>();
  
   	 Log.v("ADD GRP", "asdsad");
     models=dbOpenHelper.getFavListsItems();
 	
    	 String[] mGroupsNames=getResources().getStringArray(R.array.item_categories);
	 
   	  for (int i=0;i<mGroupsNames.length;i++){
   		List<ShopItemData> tItems = new ArrayList<ShopItemData>();
   		 Log.v("ADD GRP", mGroupsNames[i]);
   		 for (int k=0;k<models.size();k++){
   			 if (models.get(k).getGroup().equals(mGroupsNames[i])){
   				 tItems.add(models.get(k));
   				 Log.v("ADD GRP item", models.get(k).getName());
   			 }
   		 }
   		 //tItems.add(new ShopItemData("tmp",0));
   		
   		ShopItemData[] line=new ShopItemData[tItems.size()];
   		tItems.toArray(line);
   		data.add(line);
   		Log.v("ADD GRP count", String.valueOf(data.size()));
   	 }
     ShopItemData[][] dt=new ShopItemData[data.size()][];
     data.toArray(dt);
     Log.v("GRP val", String.valueOf(dt.length));

//  	  listView.invalidateViews();
     return new SampleExpandableListAdapter(context, this, dt);
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_expandablelist);
        
        dbOpenHelper= new DbOpenHelper(this);
        
        button1 = (Button)findViewById(R.id.btn1);
        adapt=loadFavoritesItem();
        expandableListView = (ExpandableListView)findViewById(R.id.expandableListView1);
        expandableListView.setAdapter(adapt); 
        
        button1.setOnClickListener(new View.OnClickListener() { 
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub 
				Bundle b=new Bundle();
				Intent intent = new Intent();
				for (int i=0;i<adapt.data.length;i++){
					for (int k=0;k<adapt.data[i].length;k++){
						if (adapt.data[i][k].getCount()>0){
							result.add(adapt.data[i][k]);
							Log.v("val", adapt.data[i][k].getName()+String.valueOf(adapt.data[i][k].getCount()));
						}
					}
				}
//				ShopItemData[] values=new ShopItemData[result.size()];
//				for (int i=0;i<result.size();i++){
//					values[i]=result.get(i);
//				}
				b.putParcelableArrayList("setedVals", result);
				intent.putExtras(b);
				setResult(result.size(), intent);
				finish();
			}
		});
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.voice_shoper_main, menu);
        return true;
    }
    
}
